package com.liunian.algorithmstudy.listnode;

public class ReverseList206 {

    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode reverseList(ListNode head) {
        return reverseNode(head, null);
    }

    private ListNode reverseNode(ListNode head, ListNode tail) {
        if (head == null) {
            return tail;
        }
        ListNode newHead = head.next;
        head.next = tail;
        return reverseNode(newHead, head);
    }

}
